from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
# 初始化driver
driver = webdriver.Chrome()
# 打开网页
driver.get('https://www.baidu.com/')
# 设置隐式等待
driver.implicitly_wait(15)
# 全屏
driver.maximize_window()
# 输入内容
driver.find_element(By.ID, 'kw').send_keys('秦忠阳的爱情故事')
# 点击搜索
driver.find_element(By.ID,'su').click()
# 点击第一个连接
driver.find_elements(By.CLASS_NAME,'tts-b-hl')[0].click()
# 句柄切换
win = driver.window_handles       # 获取所有的句柄
# 跳转到最后一个句柄
print(win)
driver.switch_to.window(win[1])
# 获取文章标题
text = driver.find_element(By.CLASS_NAME,"sKHSJ").text
print(text)
# 点击第一个链接新闻
driver.find_elements(By.CLASS_NAME,"cxgWx")[0].click()
# 进入新的窗口
win = driver.window_handles       # 获取所有的句柄
print(win)
driver.switch_to.window(win[2])
# 获取文章标题
text = driver.find_element(By.CLASS_NAME,"sKHSJ").text
print(text)
# 句柄切换
win = driver.window_handles       # 获取所有的句柄
print(win)
driver.switch_to.window(win[1])
# 点击第二个新闻链接
driver.find_elements(By.CLASS_NAME,"cxgWx")[1].click()

sleep(10)